package com.hotel.tv.web.admin.controllers;

import com.autumn.runtime.session.AutumnSessionManager;
import com.autumn.security.constants.UserStatusConstants;
import com.autumn.zero.authorization.entities.common.UserRole;
import com.hotel.tv.constants.HotelUserRoleConstants;
import com.hotel.tv.constants.HotelUserTypeConstant;
import com.hotel.tv.constants.SysResourcesTypeConstants;
import com.hotel.tv.entities.auth.HotelRole;
import com.hotel.tv.entities.auth.HotelUser;
import com.hotel.tv.services.HotelAuthorizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * 会话管理控制器
 * <p>
 * </p>
 *
 * @description TODO
 * @author: 老码农
 * @create: 2020-04-08 07:13
 **/
@RestController
@RequestMapping("/session/manager")
@Api(tags = "会话管理")
@RequiresRoles(value = {HotelUserRoleConstants.ADMINISTRATORS})
public class SessionManagerController {

    private final AutumnSessionManager sessionManager;

    public SessionManagerController(AutumnSessionManager sessionManager) {
        this.sessionManager = sessionManager;
    }

    /**
     * @return
     */
    @PostMapping("/online/sessions")
    @ApiOperation(value = "在线会话数")
    public Long onLineSessions() {
        return sessionManager.onLineSessions();
    }

    /**
     * @return
     */
    @PostMapping("/online/users")
    @ApiOperation(value = "在线用户数")
    public Long onLineUsers() {
        return sessionManager.onLineUsers();
    }

    /**
     * @return
     */
    @PostMapping("/delete/session")
    @ApiOperation(value = "删除会话用户")
    public void deleteUser(Long userId) {
        sessionManager.deleteSessionUser(userId);
    }

    /**
     * @return
     */
    @PostMapping("/delete/session/all")
    @ApiOperation(value = "删除所有会话用户")
    public void deleteAllUser() {
        sessionManager.deleteAllSessionUser();
    }

    /**
     * @return
     */
    @PostMapping("/syn/session")
    @ApiOperation(value = "同步用户")
    public void synSession() {
        sessionManager.synSession();
    }

    @Autowired
    private HotelAuthorizationService service;

    /**
     * 添酒店用户(仅测试)
     *
     * @param userName
     */
    @PostMapping("/user/hotel/add")
    @ApiOperation(value = "添酒店用户")
    @Transactional(rollbackFor = Exception.class)
    public void addHotel(String userName) {

        Long orgId = 1000L;  //取得酒店的id

        HotelRole role = new HotelRole();
        role.setOrganizeId(orgId);
        role.setUserType(HotelUserTypeConstant.HOTEL_ADMIN);
        role.setName("管理员");
        role.setSortId(1);
        role.setSummary("测试");

        service.addRole(role);

        service.authorizeByRoleAllPermission(role.getId(), SysResourcesTypeConstants.HOTEL_RESOURCES);

        HotelUser user = new HotelUser();
        user.setOrganizeId(orgId);
        user.setUserType(HotelUserTypeConstant.HOTEL_ADMIN);
        user.setUserName(userName);
        user.setPassword("123456");
        user.setRealName(userName);
        user.setSex("男");
        user.setStatus(UserStatusConstants.NORMAL);
        user.setRoles(new ArrayList<>(1));
        UserRole userRole = new UserRole();
        userRole.setRoleId(role.getId());
        user.getRoles().add(userRole);

        service.addUser(user, false);
    }
}
